from django.db import models
from django.contrib.auth.models import User


class Server(models.Model):
    """MCP Server 模型"""
    ACCESS_MODE_CHOICES = [
        ('LOCAL', 'Local'),
        ('SSE', 'SSE'),
        ('HTTP', 'HTTP'),
    ]
    
    ZONE_CHOICES = [
        ('YELLOW', '黄区'),
        ('GREEN', '绿区'),
        ('COMMON', '通用区'),
    ]
    
    id = models.CharField(max_length=255, primary_key=True)
    name = models.CharField(max_length=255)
    name_cn = models.CharField(max_length=255, null=True, blank=True)
    icon = models.URLField(null=True, blank=True)
    provider = models.CharField(max_length=255, null=True, blank=True)
    description = models.TextField(null=True, blank=True)
    description_cn = models.TextField(null=True, blank=True)
    category = models.JSONField(default=list, null=True, blank=True)
    category_cn = models.JSONField(default=list, null=True, blank=True)
    tags = models.JSONField(default=list, null=True, blank=True)
    is_official = models.BooleanField(default=False)
    is_internal = models.BooleanField(default=False)
    auth_method = models.CharField(max_length=100, null=True, blank=True)
    dev_language = models.CharField(max_length=255, null=True, blank=True)
    from_site = models.CharField(max_length=100, null=True, blank=True)
    from_site_url = models.URLField(null=True, blank=True)
    internal_doc_url = models.URLField(null=True, blank=True, help_text="华为内部使用文档链接")
    server_config = models.TextField(null=True, blank=True)
    env_schema = models.JSONField(default=dict, null=True, blank=True)
    tools = models.JSONField(default=list, blank=True, null=True)
    readme = models.TextField(null=True, blank=True)
    readme_cn = models.TextField(null=True, blank=True)
    verified = models.BooleanField(default=False)
    sse_url = models.URLField(null=True, blank=True)
    http_url = models.URLField(null=True, blank=True)
    access_mode = models.CharField(max_length=10, choices=ACCESS_MODE_CHOICES, default='LOCAL')
    zone = models.CharField(max_length=10, choices=ZONE_CHOICES, null=True, blank=True, 
                           help_text="区域标识，仅内部MCP Server使用")
    is_deleted = models.BooleanField(default=False, help_text="标记是否已删除")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    def __str__(self):
        return self.name


class Comment(models.Model):
    """评论模型"""
    server = models.ForeignKey(Server, related_name='comments', on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    def __str__(self):
        return f"Comment by {self.user.username} on {self.server.name}"


class Reply(models.Model):
    """回复模型"""
    comment = models.ForeignKey(Comment, related_name='replies', on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return f"Reply by {self.user.username} to {self.comment.user.username}'s comment"
